﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomsSystem.BLL;
using CustomsSystem.Model;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Maticsoft.Common;
using System.Data;

namespace WEB.workflow
{
    public partial class workflowHandle : System.Web.UI.Page
    {
        static CustomsSystem.Model.workflow model;
        static CustomsSystem.Model.workflow modelBak;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ID"] != null)   //修改
                {
                    int id = Convert.ToInt32(Request.QueryString["ID"]);
                    model = new CustomsSystem.Model.workflow();
                    modelBak = new CustomsSystem.Model.workflow();
                    CustomsSystem.BLL.WorkflowHandleManage bll = new CustomsSystem.BLL.WorkflowHandleManage();
                    model = bll.GetModel(id);
                    BindBusData(ddlNext, model.infoWKID.ToString());
                    lblCurBusName.Text = model.busName;
                    UserManage Userbll=new UserManage();
                    Users User = Userbll.GetModel(model.jobEmpNo);
                    lblCurJobEmpNo.Text = User.UserName;
                    //默认是流程的下一步
                    ddlNext.SelectedIndex = ddlNext.Items.IndexOf(ddlNext.Items.FindByText(model.busName));
                    if (ddlNext.SelectedIndex != ddlNext.Items.Count - 1)
                    {
                        //int selectIndex = ddlNext.SelectedIndex;
                        ddlNext.SelectedIndex++;
                    }

                }
                txtHandle.InnerText = "已完成";
            }
        }

        protected void btnHandle_Click(object sender, EventArgs e)
        {
            //先做传递检测

            WorkflowHandleManage bll=new WorkflowHandleManage();
            WorkflowManage bllSet=new WorkflowManage();
            SheetInfoManage bllSheet = new SheetInfoManage();
            if (model != null)
            {
                modelBak = CloneOf<CustomsSystem.Model.workflow>(model); 
                //修改原纪录
                string strEmpNo = Session["EmpNo"].ToString();
                model.doDate = DateTime.Now;

                model.doEmpNo = strEmpNo; //当前登录角色
                if (txtHandle.InnerText == "") txtHandle.InnerText = "已完成";
                model.doMark=txtHandle.InnerText;
                model.toDate = DateTime.Now;
                //判断报关还是报检
                int type = 0;
                if(model.infoWKID!=null) type = model.infoWKID.Value;
                Info_workflowMaster modelMaster = bllSet.GetModelMaster(type);
                if(modelMaster!=null)
                {
                    if(modelMaster.sheetTypeID==1) type = 1;
                    else if (modelMaster.sheetTypeID == 2) type = 2;
                }
                int IntimeFlag = bll.GetIntime(model.sheetID,type);//判断及时度
                model.ontimeType = IntimeFlag; 
                if (!bll.Update(model))  //更新、完成本条纪录
                {
                    MessageBox.Show(this, "提交出错，请检查输入是否正确");
                    bll.Update(modelBak); //还原
                    return;
                }
                //插入新纪录
                //ddl选中的是下一环节
                model.busID = Convert.ToInt32(ddlNext.SelectedValue); //下一环节的ID
                model.busName = ddlNext.SelectedItem.Text;
                DataSet ds = bllSet.GetList("infoWKTypeID='" + model.infoWKID + "' and busType=" + model.busID);
                model.jobNO = ds.Tables[0].Rows[0]["jobNo"].ToString();
                model.inputEmpNo = model.doEmpNo;
                model.inputDate = DateTime.Now;
                //初始化
                model.receiveMark = 0;
                model.jobEmpNo = null;
                model.recDate = null;
                model.toDate = null;
                model.doMark = null;
                model.doEmpNo = null;
                model.doDate = null;
                model.ontimeType = null;
                //传递到完结 为绿灯
                if (model.busID == 99) model.ontimeType = 1;

                int newWKID = bll.Add(model);  //插入数据，获取新标识
                if (newWKID == 0)
                {
                    MessageBox.Show(this,"提交出错");
                    bll.Update(modelBak); //还原
                    return;
                }
                //修改委托单表的冗余字段
                CustomsSystem.Model.sheetInfo modelSheet = bllSheet.GetModel(model.sheetID);
                //判断type，报关、报检
                if (type == 1)
                {
                    modelSheet.busIDbg = model.busID;
                    modelSheet.busNamebg = model.busName;
                    modelSheet.jobNobg = model.jobNO;

                    modelSheet.jobEmpNobg = model.inputEmpNo;
                    modelSheet.infoWKIDbg = newWKID;
                }
                else if (type == 2)
                {
                    modelSheet.busIDbj = model.busID;
                    modelSheet.busNamebj = model.busName;
                    modelSheet.jobNobj = model.jobNO;

                    modelSheet.jobEmpNobj = model.inputEmpNo;
                    modelSheet.infoWKIDbj = newWKID;
                }
                if (bllSheet.Update(modelSheet))
                {
                    MessageBox.ShowAndRedirect(this, "传递成功！", "workflowView.aspx");
                }
            }
        }

        /// <summary>
        /// 获取制定代码类型绑定到控件上
        /// </summary>
        /// <param name="ddl"></param>
        private void BindBusData(DropDownList ddl, string WKTypeID)
        {
            WorkflowManage wfBll = new WorkflowManage();
            List<Info_workflow> wfModels = wfBll.GetModelList("infoWKTypeID=" + WKTypeID +" and States =0" );
            foreach (Info_workflow aWf in wfModels)
            {
                ddl.Items.Add(new ListItem(aWf.busName, aWf.busType.ToString()));
            }
        }


        /// <summary>
        /// 泛型深拷贝
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="serializableObject"></param>
        /// <returns></returns>
        public static T CloneOf<T>(T serializableObject)
        {
            object objCopy = null;

            MemoryStream stream = new MemoryStream();
            BinaryFormatter binFormatter = new BinaryFormatter();
            binFormatter.Serialize(stream, serializableObject);
            stream.Position = 0;
            objCopy = (T)binFormatter.Deserialize(stream);
            stream.Close();
            return (T)objCopy;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("workflowView.aspx");
        }

        protected void btnEditSHeetInfo_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.QueryString["SHEETID"]);
            Response.Redirect("~/sheetInfo/sheetEdit.aspx?ID=" + id);
        }

        
    }
}